package com.example.springkafka.api;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @Date: 2018/11/6 20:25
 * @Description: 生产者
 */
public class KafkaProducerDemo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers","192.168.3.221:9092");
        properties.setProperty("key.serializer", StringSerializer.class.getName());
        properties.setProperty("value.serializer", StringSerializer.class.getName());
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
        String topic = "message"; // 主题
        Integer partition = 0; // 指定分区
        long timeMillis = System.currentTimeMillis(); // 毫秒值 15分钟
        String key = "key-message"; // key
        String value = "value-message"; // value
        // 创建ProducerRecord
        ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic, partition, timeMillis, key, value);
        // 生产消息
        kafkaProducer.send(producerRecord);
        kafkaProducer.close();
    }
}
